﻿Imports PTIC_GestionBDD
Imports PTIC_Util
Imports Actuariat
Imports OMANE.Common.Model.Parametrage

''' <summary>
''' Segmentation retenue pour l'étude
''' </summary>
''' <remarks></remarks>
Public Class Segmentation
    Inherits BaseNotifyPropertyChangeComponentModel
    Implements ISegmentation

#Region "Variables de classe"
    Protected _lVarSegmentationPossNonRetenue As New ObservableDictionary(Of String, Correspondance)
    Protected _lVarSegmentationRetenue As New ObservableDictionary(Of String, Correspondance)
    'Protected m_ModeleOMANE As ModelTauxGarantis
    Private m_TraitementEntree As ITraitement
    Private m_TraitementBDD As ITraitement
    Private m_ListeParametres As ParametresList
#End Region

#Region "Création d'une instance"
    ''' <summary>
    ''' Constructeur
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByVal TraitementEntree As ITraitement,
                   ByVal TraitementBDD As ITraitement,
                   ByVal ListeParametres As ParametresList)
        'm_ModeleOMANE = Model
        m_TraitementBDD = TraitementBDD
        m_TraitementEntree = TraitementEntree
        m_ListeParametres = ListeParametres
        _lVarSegmentationPossNonRetenue = New ObservableDictionary(Of String, Correspondance)
        _lVarSegmentationRetenue = New ObservableDictionary(Of String, Correspondance)
        Dim traitImportPrepEncours1 As RequeteBDD = m_TraitementEntree
        For Each champs As Correspondance In traitImportPrepEncours1.ListeChampsSortie.Values
            AddHandler champs.EtatChanged, AddressOf onEtatChampsImportPrepEncours1_Changed
        Next
    End Sub
#End Region

#Region "Propriétés"

    ''' <summary>
    ''' Variables utilisables mais non utilisées en segmentation
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property PossibleNonRetenue As ObservableDictionary(Of String, Correspondance) Implements ISegmentation.PossibleNonRetenue
        Get
            Return _lVarSegmentationPossNonRetenue
        End Get
    End Property
    Private Sub SetPossibleNonRetenue(ByVal Value As ObservableDictionary(Of String, Correspondance))
        OnPropertyChanging("PossibleNonRetenue")
        _lVarSegmentationPossNonRetenue = Value
        OnPropertyChanged("PossibleNonRetenue")
    End Sub

    ''' <summary>
    ''' Variables utilisées en segmentation
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Retenue As ObservableDictionary(Of String, Correspondance) Implements ISegmentation.Retenue
        Get
            Return _lVarSegmentationRetenue
        End Get
    End Property
    Private Sub SetRetenue(ByVal value As ObservableDictionary(Of String, Correspondance))
        OnPropertyChanging("Retenue")
        _lVarSegmentationRetenue = value
        OnPropertyChanged("Retenue")
    End Sub

#End Region

#Region "Gestion des variables de segmentation"

    ''' <summary>
    ''' Modifie la liste des correspondances non retenues mais qu'il est possibles de retenir en segmentation
    ''' </summary>
    ''' <param name="listeSegments">Liste de correspondances à ajouter/supprimer</param>
    ''' <param name="operation">Opération à effectuer</param>
    ''' <remarks></remarks>
    Public Sub modifSegPossibleNonRetenue(ByVal listeSegments As List(Of Correspondance), _
                                           ByVal operation As operationCollection) Implements ISegmentation.modifSegPossibleNonRetenue

        If operation = operationCollection.REPLACE Then
            SetPossibleNonRetenue(New ObservableDictionary(Of String, Correspondance))
        End If

        If operation = operationCollection.REPLACE Or operation = operationCollection.ADD Then
            For Each corresp As Correspondance In listeSegments
                If corresp.description <> descIdOption And corresp.description <> descSexeAssu Then
                    PossibleNonRetenue.Add(corresp.description, corresp)
                End If
            Next
        ElseIf operation = operationCollection.REMOVE Then
            For Each corresp As Correspondance In listeSegments
                If PossibleNonRetenue.ContainsKey(corresp.description) Then
                    PossibleNonRetenue.Remove(corresp.description)
                End If
            Next
        End If
    End Sub

    ''' <summary>
    ''' Modifie la liste des correspondances retenues en segmentation
    ''' </summary>
    ''' <param name="listeSegments">Liste de correspondances à ajouter/supprimer</param>
    ''' <param name="operation">Opération à effectuer</param>
    ''' <remarks></remarks>
    Public Sub modifSegRetenue(ByVal listeSegments As List(Of Correspondance), _
                               ByVal operation As operationCollection) Implements ISegmentation.modifSegRetenue

        'Lorsque la segmentation retenue est modifiée, le traitement permettant d'extraire la segmentation
        'est modifiée également
        Dim reqSegmentation As RequeteBDD = m_TraitementBDD

        If operation = operationCollection.REPLACE Then
            'Mise à jour de la collection
            SetRetenue(New ObservableDictionary(Of String, Correspondance))
            For Each corresp As Correspondance In reqSegmentation.ListeChampsSortie.Values
                'Mise à jour du traitement + refaire la jointure avec le traitement suivant
                reqSegmentation.removeSELECT(corresp.description)
            Next
            RaiseEvent SegmentationRetenueChanged(Me, New EventArgs)
        End If

        If operation = operationCollection.ADD Or operation = operationCollection.REPLACE Then
            For Each corresp As Correspondance In listeSegments
                'Mise à jour de la collection
                Retenue.Add(corresp.description, corresp)
                'Mise à jour du traitement
                reqSegmentation.addSELECT(corresp.traitement.nom, corresp.description)
            Next
            RaiseEvent SegmentationRetenueChanged(Me, New EventArgs)
        ElseIf operation = operationCollection.REMOVE Then

            'Ici, il faut vérifier que le(s) segments que l'on souhaite supprimer n'apparaissent pas dans un paramètre
            'Dim univ As ModelTauxGarantis = m_ModeleOMANE
            Dim modif As Boolean = False
            Dim paramModif As New List(Of IParametre)

            For Each corresp As Correspondance In listeSegments

                Dim CorrespASupprimer As Boolean = False
                Dim lstParam As New List(Of IParametre)

                If Retenue.ContainsKey(corresp.description) Then
                    If ((corresp.description <> descIdOption And corresp.description <> descSexeAssu) _
                        Or corresp.etat <> Drawing.Color.Green) Then

                        For Each p As IParametre In m_ListeParametres.Values
                            If p.ListeSegments.Contains(corresp.description) Then
                                lstParam.Add(p)
                            End If
                        Next
                        If lstParam.Count <> 0 Then
                            Dim strParam As String = ""
                            For Each p As IParametre In lstParam
                                strParam = strParam & "   - " & p.Description & vbCrLf
                            Next
                            Dim res As Integer = 6
                            If corresp.etat <> Drawing.Color.Red Then
                                res = MsgBox("La variable de segmentation " & corresp.description & " est utilisée par le(s) paramètre(s) suivant(s) : " & vbCrLf & _
                                             strParam & _
                                             "Etes-vous sûr de vouloir la supprimer ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo)
                            End If
                            If res = 6 Then
                                CorrespASupprimer = True
                            Else
                                CorrespASupprimer = False
                            End If
                        Else
                            CorrespASupprimer = True
                        End If

                    Else
                        MsgBox("Vous ne pouvez pas supprimer " & corresp.description & " de la segmentation")
                        CorrespASupprimer = False
                    End If

                End If

                If CorrespASupprimer Then
                    'Mise à jour de la collection
                    Retenue.Remove(corresp.description)
                    'Mise à jour du traitement
                    reqSegmentation.removeSELECT(corresp.description)
                    For Each p As IParametre In lstParam
                        p.ListeSegments.Remove(corresp.description)
                        If Not paramModif.Contains(p) Then paramModif.Add(p)
                    Next
                    modif = True
                End If

            Next
            For Each p As IParametre In paramModif
                p.MAJValeurs()
            Next
            If modif Then RaiseEvent SegmentationRetenueChanged(Me, New EventArgs)
        End If

    End Sub

#End Region

#Region "Actualisations liées aux observations du traitement entrée"

    ''' <summary>
    ''' Changement d'état d'une correspondance du traitement préparatoire 1 de la table d'encours
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub onEtatChampsImportPrepEncours1_Changed(ByVal sender As Object, ByVal e As EventArgs)
        'Modification de la liste de segmentation possible et non retenue
        Dim correspModifiee As Correspondance = CType(sender, Correspondance)
        Dim lSegAajouter As New List(Of Correspondance)
        Dim operation As operationCollection

        lSegAajouter.Add(correspModifiee)

        If correspModifiee.etat = System.Drawing.Color.Green Then
            operation = operationCollection.ADD
            If correspModifiee.description = descIdOption Or correspModifiee.description = descSexeAssu Then
                'Ajouter les correspondances dans la segmentation retenue
                modifSegRetenue(lSegAajouter, operationCollection.ADD)
                RaiseEvent SegmentationRetenueChanged(Me, New EventArgs)
            Else
                modifSegPossibleNonRetenue(lSegAajouter, operation)
                RaiseEvent SegmentationPossibleNonRetenueChanged(Me, New EventArgs)
            End If
        Else
            operation = operationCollection.REMOVE
            'Faire un test pour voir si le segment concerné appartient à la segmentation retenue ou non retenue
            'Si il appartient à la segmentation retenue, le supprimer
            modifSegRetenue(lSegAajouter, operation)
            modifSegPossibleNonRetenue(lSegAajouter, operation)
            RaiseEvent SegmentationPossibleNonRetenueChanged(Me, New EventArgs)
            'Si la correspondance qui vient d'être modifiée était retenue dans la segmentation
        End If

    End Sub

#End Region

    Public Event SegmentationPossibleNonRetenueChanged As EventHandler Implements ISegmentation.SegmentationPossibleNonRetenueChanged
    Public Event SegmentationRetenueChanged As EventHandler Implements ISegmentation.SegmentationRetenueChanged

End Class